"""
auth ：xgt-python
datatime : 2021/11/14
spider 美团商家数据
"""
# 导入相应的模块
import requests
import json
import pprint  # 格式化输出模块
import csv

f = open('美团按摩店.csv',mode='a',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=[
    '店铺名字',
    '店铺类型',
    '店铺商圈',
    '店铺评分',
    '店铺评论量',
    '人均消费',
    '详情页',
])
csv_writer.writeheader() # 写入表头

url = 'https://apimobile.meituan.com/group/v4/poi/pcsearch/70'
# 因为他是字典数据类型
data = {
    'uuid': '050adbe3beb947e4a425.1637060828.1.0.0',
    'userid': '3501246195',
    'limit': '32',
    'offset': '32',
    'cateId': '-1',
    'q': '按摩',
    'token': 'UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g',
}
# User-Agent 表示浏览器基本信息
# Cookie 用户信息，常用于检测是否正确
headers = {
    'Cookie': 'uuid=050adbe3beb947e4a425.1637060828.1.0.0; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_cuid=17d286de1e2c8-0e17b0d2dcd0a2-57b1a33-144000-17d286de1e2c8; mtcdn=K; lt=UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g; u=3501246195; n=Kog850612235; token2=UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g; unc=Kog850612235; ci=70; rvct=70%2C1; IJSESSIONID=node0tph4qqv9vc4j143r3bc9n1drf26749037; iuuid=A49F810356BACDF939875ACDEC2FEECA92598334624D9576783E99816D0697EB; isid=UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g; oops=UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g; mt_c_token=UL_aW3Jvr8wOInhe6BKoDqdj1FUAAAAAKw8AAF5ngM0FW-YDVJ_Y69OYwdXivniB7sgCNW6U1eMBgiHSNW7e5OldBEinqDiZ8vo97g; logintype=normal; cityname=%E9%95%BF%E6%B2%99; _lxsdk=A49F810356BACDF939875ACDEC2FEECA92598334624D9576783E99816D0697EB; webp=1; i_extend=H__a100005__b1; idau=1; latlng=30.964589,117.853103,1637061254398; __utma=74597006.1884542126.1637061254.1637061254.1637061254.1; __utmc=74597006; __utmz=74597006.1637061254.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=74597006.1.10.1637061254; firstTime=1637061518184; _lxsdk_s=17d286de1e2-0fe-4df-aa9%7C%7C210',
    'Referer':'https://chs.meituan.com/', # 防盗链,告诉服务器我们发送请求的url请求是从哪里跳转过来的
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}

response = requests.get(url=url,headers=headers,params=data)
# pprint.pprint(response.json())
# print(type(response.json()))  # 返回的是dict数据类型
# print(response.text)
# print(type(response.text))  # 返回的是字符串数据类型


# 解析数据 提取我们想要的一些数据内容
# json字典数据 如何提取数据内容  字典取值
searchResult = response.json()['data']['searchResult']
# for 遍历提取列表中的每一个元素内容
for index in searchResult:
    # pprint.pprint(index)
    href=f'https://www.meituan.com/xiuxianyule/{index["id"]}/'
    dit = {
        '店铺名字':index['title'],
        '店铺类型':index['backCateName'],
        '店铺商圈':index['areaname'],
        '店铺评分':index['avgscore'],
        '店铺评论量':index['comments'],
        '人均消费':index['avgprice'],
        '详情页':href
    }
    csv_writer.writerow(dit)
    print(dit)
    
    
    
    
    
    

















